package com.pro.controller;

import entity.Dto;
import entity.User1;
import entity.utils.DtoUtil;
import service.User1Service;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (User1)表控制层
 *
 * @author makejava
 * @since 2021-06-30 08:59:25
 */
@RestController
@RequestMapping("api/user1")
public class Controller {
    /**
     * 服务对象
     */
    @Resource
    private User1Service user1Service;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public User1 selectOne(@RequestParam("id") Integer id) {
        return this.user1Service.queryById(id);
    }
    @GetMapping("insert")
    public User1 insert(User1 user1){
        return this.user1Service.insert(user1);
    }
    @PostMapping("login")
    public User1 queryByName(String name, String password) {
        User1 user1 = new User1();
        user1.setName(name);
        user1.setPassword(password);
        List<User1> list = user1Service.queryAll(user1);
        if (list.isEmpty()){
            return null;
        }
        return list.get(0);
    }

    @PostMapping("logins")
    public Dto login(String name, String password){
        if (name == null || name.equals("")){
            return DtoUtil.returnError(503,"用户名未输入");
        }
        User1 user1 = user1Service.queryByName(name, password);
        if (user1 == null) {
            return DtoUtil.returnError(504,"账号密码错误");
        }
        return DtoUtil.returnSuccessData(user1);
    }
}
